package com.Camellia.zy_video.controller;


import com.Camellia.zy_video.service.CommentService;
import com.Camellia.zy_video.utils.QiniuOssUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.Camellia.zy_video.entity.Category;
import com.Camellia.zy_video.entity.User;
import com.Camellia.zy_video.entity.Video;
import com.Camellia.zy_video.service.CategoryService;
import com.Camellia.zy_video.service.UserService;
import com.Camellia.zy_video.service.VideoService;
import com.Camellia.zy_video.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author ZhouYu
 * @version 1.0
 * 视频业务接口
 */
@RequestMapping("/video")
@RestController
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        //分页查询视频资源
        List<Video> list = videoService.list(new Page<Video>(page, limit));
        list.forEach(v->{
            // 获取作者id
            Integer uid = v.getUid();
            // 获取频道id
            Integer cid = v.getCid();
            //根据作者id查询作者
            User user = userService.getById(uid);
            Category category = categoryService.getById(cid);
            //将视频的作者和频道设置到视频对象中
            v.setUser(user);
            v.setCategory(category);
        });
        //查询总记录数
        long count = videoService.count();
        return R.ok("查询成功").setData(list).setCount(count);
    }

    /**
     * 查询频道
     * @return
     */
    @RequestMapping("/categories")
    public R listCategories(){
        List<Category> list = categoryService.list();
        return R.ok("查询成功").setData(list);
    }

    /**
     * 视频上传
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file) {
        try{
            //对上传的文件生成一个随机uid
            String key = UUID.randomUUID().toString();
            //获取文件输入流
            InputStream in = file.getInputStream();
            //将文件上传到七牛OSS
            QiniuOssUtils utils = new QiniuOssUtils();
            String url = utils.upload(in,key);
            return R.ok("上传成功").setData(url);
        }catch (Exception e){
        return R.fail("上传失败");
        }
    }
    @RequestMapping("/add")
    public  R add(Video video){
        boolean b=videoService.save(video);
        return  b ? R.ok("添加成功") : R.fail("添加失败");
    }
    @RequestMapping("/delete")
    public R delete(Integer id){
        //根据id删除视频的评论
        categoryService.remove(Wrappers.query(Category.class).eq("vid",id));
        //删除OSS视频文件
        QiniuOssUtils utils = new QiniuOssUtils();
        String url = videoService.getById(id).getUrl();
        String key = url.substring(url.lastIndexOf("/")+1);
        utils.delete(key);
        //删除用户收藏视频
        return videoService.removeById(id) ? R.ok("删除成功") : R.fail("删除失败");
    }
}
